package com.gemalto.Kryptik.Gui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.*;



public class KeyPaddingAlgoPanel extends KeyedAlgoPanel implements ActionListener {

//	private KeyedAlgo algo;
	private CountedComboBox keyCombo;
	private int historyMax;
	private JRadioButton b4,b5,b6,b7;

	/* Load keys from ini-file.
	 * Maximum HISTORY_MAX keys is loaded. If there is no HISTORY_MAX,
	 * we will set it to the default value 5.
	 */
/*	private void getHistory() {
	   String key;
	   int i;

	   historyMax = Kryptik.getIni().getIntValue(algo.toString(),"HISTORY_MAX");
	   if (historyMax>-1){
	   	for (i=1;i<=historyMax;i++){
			key = Kryptik.getIni().getValue(algo.toString(),"KEY_"+i);
			if (key!=null) keyCombo.insertItemAt(key,i-1);
		}
	   } else {
		// There is no HISTORY_MAX, lets set default value 5
	    	Kryptik.getIni().setValue(algo.toString(),"HISTORY_MAX",5);
		historyMax=5;
	   }	
	}

*/



	public KeyPaddingAlgoPanel(KeyedAlgo algo){

	 	super(algo,null);
	}





	public void initGui(){

		super.initGui();

		JPanel padPanel=new JPanel();
		padPanel.setBorder(BorderFactory.createTitledBorder(Util.getTranslation("GENALGO_PADDING")));
		padPanel.setLayout(new BoxLayout(padPanel,1));
		padPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		padPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 
		padPanel.setMinimumSize(new Dimension(50, 50)); 


		b4 = new JRadioButton("\"FF\" (Simera2)");
		b4.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
		b4.setMinimumSize(new Dimension(20, 20)); 
		b4.setPreferredSize(new Dimension(20, 20));
              	b4.setActionCommand("PADD-FF");
		b4.addActionListener(this);
		b4.setSelected(true);

		b5 = new JRadioButton("\"00\" ISO 9797-M1 (Simera3)");
		b5.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
		b5.setMinimumSize(new Dimension(20, 20)); 
		b5.setPreferredSize(new Dimension(20, 20));
              	b5.setActionCommand("PADD-00");
		b5.addActionListener(this);
		b5.setSelected(false);


		b7 = new JRadioButton("\"80\" ISO 9797-M2");
		b7.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
		b7.setMinimumSize(new Dimension(20, 20)); 
		b7.setPreferredSize(new Dimension(20, 20));
              	b7.setActionCommand("PADD-80");
		b7.addActionListener(this);
		b7.setSelected(false);



		b6 = new JRadioButton("PKCS#5");
		b6.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
		b6.setMinimumSize(new Dimension(20, 20)); 
		b6.setPreferredSize(new Dimension(20, 20));
              	b6.setActionCommand("PADD-PKCS5Padding");
		b6.addActionListener(this);


		ButtonGroup group2 = new ButtonGroup();
		group2.add(b4);
		group2.add(b5);
		group2.add(b7);
		group2.add(b6);

		padPanel.add(b4);
		padPanel.add(b5);
		padPanel.add(b7);
		padPanel.add(b6);



		bottomPanel.add(padPanel);
		//add(Box.createVerticalGlue());

	}



	/* If the key is not in the ComboBox, we will inser it.
	 * History list in INI file is updated.
	 */
/*	private void setKey(String key){
	     	// Add only new key ..
     		boolean newKey = true;
     		for (int i=0;i<keyCombo.getItemCount();i++){
			if (((String)keyCombo.getItemAt(i)).equals(key)){
				newKey = false;
				break;
			}
     		}

     		if ( newKey) {
			keyCombo.insertItemAt(key,0);
     		}


     		// reset key list ...
     		for (int i=0;i<keyCombo.getItemCount();i++){
			if (i>=historyMax) break;
			Kryptik.getIni().setValue(algo.toString(), "KEY_"+(i+1), (String)keyCombo.getItemAt(i));
     		}
	
		// ... and finally set key for this algo
		//algo.setKey(new RawSecretKey(algo.getCipherName(),Hex.fromString(key)));
		algo.setKey(key);

	}

*/
	public void actionPerformed(ActionEvent e){
		super.actionPerformed(e);
		
		if (e.getActionCommand().substring(0,4).equals("PADD")){
			((GenericAlgo)algo).setPadding(e.getActionCommand().substring(5));
		} //else if (e.getActionCommand().substring(0,4).equals("KEYB")){
		  //	setKey((String)keyCombo.getEditor().getItem());
		  //}
	}

	public void update(){
		super.update();
		String mode=((GenericAlgo)algo).getPadding();
	 	if (mode.equals("FF")){
			b4.setSelected(true);
		} else if (mode.equals("00")){
			b5.setSelected(true);
		} else if (mode.equals("PKCS5Padding")) {
			b6.setSelected(true);
		}


	}


}	                         
